<template>
  <ga-modal
    v-model:visible="visible"
    title-align="start"
    :width="600"
    @close="handleCancel(formRef)"
  >
    <template #title>{{ curId ? "编辑" : "创建" }}</template>
    <ga-spin style="width: 100%" :loading="loading">
      <ga-form ref="formRef" :model="formData">
        <ga-form-item
          field="type"
          label="名称"
          label-col-flex="70px"
          :rules="[RULE.NOT_NULL]"
          :validate-trigger="['change', 'input']"
        >
          <ga-input v-model="formData.name" placeholder="请输入名称" />
        </ga-form-item>
        <ga-form-item field="type" label="存储类型" label-col-flex="70px">
          <ga-radio-group type="button" v-model="formData.type" size="small">
            <ga-radio v-for="(item, idx) in typeList" :key="idx" :value="item.value">
              {{ item.label }}
            </ga-radio>
          </ga-radio-group>
        </ga-form-item>
        <ga-form-item field="enable" label="是否启用" label-col-flex="70px">
          <ga-switch size="mini" v-model="formData.enable" />
        </ga-form-item>
        <ga-form-item field="protocol" label="存储协议" label-col-flex="70px">
          <ga-select
            v-model="formData.protocol"
            size="small"
            placeholder="请选择存储协议"
          >
            <ga-option
              v-for="(item, idx) in protocolList"
              :key="idx"
              :label="item.dictLabel"
              :value="item.dictValue"
            />
          </ga-select>
        </ga-form-item>
      </ga-form>
    </ga-spin>
    <template #footer>
      <ga-button @click="visible = false">取消</ga-button>
      <ga-button type="primary" @click="handleOk">确定</ga-button>
    </template>
  </ga-modal>
</template>

<script setup>
import { ref, defineEmits, reactive } from "vue";
import { editStorageList } from "@/api/storage";
import { byDictToList } from "@/api/dataDictionary";
import { Message } from "@garco-design/web-vue";
import { RULE } from "@/constant/validators";
import useLoading from "@/hooks/loading";

const { loading, setLoading } = useLoading(false);
const formRef = ref(null);
const formData = reactive({
  name: "",
  type: "",
  enable: "",
  protocol: "",
});

const visible = ref(false);
const curId = ref("");
const typeList = ref([
  { label: "HDD磁盘", value: "hdd" },
  { label: "SSD磁盘", value: "ssd" },
]);
const open = async (val) => {
  visible.value = true;
  if (val && val?.id) {
    setLoading(true);
    fetchProtocolData();
    curId.value = val.id;
    Object.keys(formData).forEach((item) => {
      formData[item] = val[item];
    });
    setLoading(false);
  }
};
// 获取存储协议方法
const protocolList = ref([]);
const fetchProtocolData = async () => {
  try {
    const { data } = await byDictToList({
      dictType: `storage_protocol`,
      limit: -1,
    });
    protocolList.value = data?.items || [];
  } catch (error) {
    console.log(error);
  }
};
// 保存
const emits = defineEmits(["fetchData"]);
const handleOk = async () => {
  const res = await formRef.value?.validate();
  if (!res) {
    try {
      const { code } = await editStorageList(curId.value, formData);
      if (code === 20000) Message.success("操作成功");
      emits("fetchData");
    } finally {
      visible.value = false;
    }
  }
};
const handleCancel = (formEl) => {
  curId.value = "";
  formEl.resetFields();
};
defineExpose({ open });
</script>

<style lang="scss" scoped></style>
